package org.qiancizhan.UI;

import org.qiancizhan.Controller.WordController;
import org.qiancizhan.Model.POJO.WordPOJO;
import org.qiancizhan.Utils.UIFormatUtli;

import java.util.List;
import java.util.Scanner;

public class TrainUI {
    private static final WordController wordController = new WordController();
    private static final Scanner sc = new Scanner(System.in);

    public static void showIndexMenu() {
        System.out.println("单词训练\t请输入你要训练的单词数量(随机抽取)");
        String num = sc.nextLine().trim();
        if (num.equals("0")) return;
        if (Integer.parseInt(num) < 1) {
            System.out.println("请输入正确的数量");
            return;
        }
        while (true) {
            System.out.println("每道题给出翻译拼写写单词，您的帮助选项有\t");
            System.out.println(UIFormatUtli.HR);
            System.out.println("1.查看单词\t2.提示(逐渐显示单词)\t0.程序退出");
            System.out.println(UIFormatUtli.HR);
            System.out.println("每次提示都会渐渐显现整个单词");
            System.out.println("开始训练请按1，退出请按0");
            String choice = sc.nextLine().trim();
            if (choice.equals("0")) return;
            else if (choice.equals("1")) break;
            else System.out.println("无效的选项");
        }
        for (int i = 3; i > 0; i--) {
            try {
                System.out.println(String.format("%d...", i));
                Thread.sleep(1000);
            } catch (Exception e) {
                System.out.println("程序异常");
            }
        }
        System.out.println("开始训练");
        List<WordPOJO> words = wordController.getNumWords(Integer.parseInt(num));
        int count = 0;
        int tipCount = 1;
        long startTime = System.currentTimeMillis();
        while (count < words.size()) {
            UIFormatUtli.log(String.format("===第%d题===", count + 1));
            System.out.println(words.get(count).getTranslate());
            String input = sc.nextLine().trim();
            if (input.equals(words.get(count).getWord())) {
                count++;
                tipCount = 1;
                System.out.println("回答正确");
            } else if (input.equals("0")) {
                System.out.println("退出训练");
                return;
            } else if (input.equals("1")) {
                System.out.println(words.get(count).getWord());
            } else if (input.equals("2"))
                System.out.println(words.get(count).getWord().substring(0, tipCount > words.get(count).getWord().length() ? words.get(count).getWord().length() : tipCount++));
            else System.out.println("回答错误");
        }
        long totalTime = System.currentTimeMillis() - startTime;
        System.out.println("训练完成！总用时: " + (totalTime / 1000.0) + "秒");
        while (true) {
            System.out.println("是否继续训练？\t1.重新训练\t0.退出训练");
            int choice = sc.nextInt();
            if (choice == 0) return;
            else if (choice == 1) showIndexMenu();
            else System.out.println("无效的选项");
        }
    }
}
